package com.china08.yxyapi.service.manage.tec.teaching.homework.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.china08.yxyapi.api.manage.tec.teaching.homework.worklist.workinfo.analyse.HomeworkNoticeQuesController.RespModel;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReport;
import com.china08.yxyapi.entity.mg.homework.HomeworkTaskReportQues;
import com.china08.yxyapi.repository.mg.homework.HomeworkTaskReportRepository;
import com.china08.yxyapi.service.manage.tec.teaching.homework.HomeworkInfoAnalyseNoticeQuesService;

@Service
public class HomeworkInfoAnalyseNoticeQuesServiceImpl implements HomeworkInfoAnalyseNoticeQuesService {

    @Autowired
    HomeworkTaskReportRepository homeworkTaskReportRepository;

    @Override
    public List<RespModel> convertRespModel(String taskId, String classId,
            List<HomeworkTaskReportQues> homeworkTaskReportQueslist) {
        // return
        List<RespModel> respModels = new ArrayList<RespModel>();
        // 作业结果状态
        List<Integer> statusIndexs = this.statusIndexs();
        statusIndexs.forEach(statusIndex -> {
            RespModel respModel = new RespModel();
            int total = 0;
            List<String> students = new ArrayList<String>();
            // ##
            for (HomeworkTaskReportQues homeworkTaskReportQues : homeworkTaskReportQueslist) {
                if (homeworkTaskReportQues.getNotifyPassed() != null
                        && (homeworkTaskReportQues.getNotifyPassed().equals(statusIndex)
                                || homeworkTaskReportQues.getNotifyPassed() == statusIndex)) {
                    total++;
                    students.add(homeworkTaskReportQues.getStudentName());
                }
            }
            // ##
            respModel.status = this.statusName(statusIndex);
            respModel.total = total;
            respModel.students = students;
            respModels.add(respModel);
        });
        // 未交作业学生统计
        List<HomeworkTaskReport> homeworkTaskReports = homeworkTaskReportRepository
                .findByTaskIdAndClassIdOrderByFinishedDateAsc(taskId, classId);
        RespModel respModel = new RespModel();
        int total = 0;
        String status = "未交";
        List<String> students = new ArrayList<String>();
        for (HomeworkTaskReport homeworkTaskReport : homeworkTaskReports) {
            boolean blag = true;
            for (HomeworkTaskReportQues homeworkTaskReportQues : homeworkTaskReportQueslist) {
                if (homeworkTaskReport.getStudentId().equals(homeworkTaskReportQues.getStudentId())) {
                    blag = false;
                }
            }
            if (blag) {
                total++;
                students.add(homeworkTaskReport.getStudentNick());
            }
        }
        respModel.status = status;
        respModel.total = total;
        respModel.students = students;
        respModels.add(respModel);
        return respModels;
    }

    /** 作业结果 List */
    public List<Integer> statusIndexs() {
        List<Integer> statusIndexs = new ArrayList<Integer>();
        statusIndexs.add(0);
        statusIndexs.add(1);
        statusIndexs.add(2);
        statusIndexs.add(3);
        statusIndexs.add(4);
        statusIndexs.add(5);
        return statusIndexs;
    }

    /** 作业状态处理 */
    public String statusName(int index) {
        if (index == 0) {
            return "0";
        } else if (index == 1) {
            return "1";
        } else if (index == 2) {
            return "2";
        } else if (index == 3) {
            return "3";
        } else if (index == 4) {
            return "4";
        } else {
            return "5";
        }
    }
}
